import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;



public class Search {

	private PhoneLayout phone; // whole app screen (inc swipe right etc)
	
	private JButton backBut, searchBut;
	
	private JPanel cards; // cards contains all screens
	
	private HintTextField searchBox;
	
	private JPanel searchPanel, mainScreen, resultsPanel;
	
	private final String SEARCHTEXT = "Search term (e.g dinner, tag, 8/1/2011)";

	
	public Search(PhoneLayout phone){ // constructor set up panell
		this.phone = phone; // takes in the whole screen so it knows where to be assigned to
		cards = phone.cards;
		
		/* build full screen */
		searchPanel = new JPanel();
		searchPanel.setLayout(new BoxLayout(searchPanel,BoxLayout.Y_AXIS));
		searchPanel.setBackground(Color.white);
		searchPanel.setMaximumSize(new Dimension(480,800));
		searchPanel.setMinimumSize(new Dimension(480,800));
		searchPanel.setPreferredSize(searchPanel.getMaximumSize());
		
		
		/* build top navigation bar*/
		JPanel topBar = new JPanel(new BorderLayout());
		topBar.setBackground(Color.darkGray);
		topBar.setPreferredSize(new Dimension(480,80));
		searchPanel.add(topBar);		
		
		JLabel title = new JLabel("   Search");
		title.setForeground(Color.white);
		title.setFont(new Font("Arial",Font.PLAIN,25));
		topBar.add(BorderLayout.WEST, title);
		
		/* top buttons */
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));
		buttonPanel.setBackground(Color.darkGray);
		buttonPanel.setPreferredSize(new Dimension(100, 80));
		
		ButtonActionListener ButListener = new ButtonActionListener();

		backBut = new JButton(" Back ");
		backBut.addActionListener(ButListener);
		buttonPanel.add(backBut);
		
		topBar.add(BorderLayout.EAST, buttonPanel);	
		
	   /* build main screen THE MIDDLE WHITE BIT*/
		

		mainScreen= new JPanel(); 
		
		mainScreen.setBorder(new EmptyBorder(20,10,20,10)); // padding
		mainScreen.setBackground(Color.white);
		mainScreen.setPreferredSize(new Dimension(480,720));
		
		Font size20Font = new Font("Arial",Font.PLAIN,20);
		
		Dimension buttonSize = new Dimension(460,80);
		
		searchBox = new HintTextField(SEARCHTEXT);
		searchBox.setFont(size20Font);
		searchBox.setPreferredSize(new Dimension(360,60));
		
		searchBut = new JButton("Go");
		searchBut.setFont(size20Font);
		searchBut.setPreferredSize(new Dimension(60,50));
		searchBut.addActionListener(ButListener);
		
		
		resultsPanel = new JPanel();
		resultsPanel.setBackground(Color.white);
		mainScreen.add(searchBox);
		mainScreen.add(searchBut);

		mainScreen.add(resultsPanel);
		
		searchPanel.add(mainScreen);
		cards.add(searchPanel, "search");
	}
	

	private void search(String s){
		List<Appointment> cal = phone.cal.getAppointmentsBetweenDates(new CalendarDate(1,1,2011), new CalendarDate(31,12,2011));
		//System.out.println("Searching for : "+s);
		List<Appointment> results = new ArrayList<Appointment>();
		for (Appointment ap: cal){
			//System.out.println("Looking at : "+ ap.description);
			boolean matchedDate = (AddEvent.parseDate(s)!=null && ap.date.equals(AddEvent.parseDate(s)));
			if (ap.description.toLowerCase().contains(s.toLowerCase()) || ap.category.contains(s) || matchedDate ){
				System.out.println(ap);
				results.add(ap);
			}
//			else if (AddEvent.parseDate(s)!=null && ap.date.equals(AddEvent.parseDate(s))){
//				System.out.println(ap);
//				results.add(ap);
//			}
				
			
		}
		if (results.size()>0){ // if found some matches
			JPanel toAdd = AgendaView.createAppPanel(results);
			toAdd.setBackground(Color.white);
			resultsPanel.setPreferredSize(new Dimension(480,400));
			resultsPanel.removeAll();
			resultsPanel.add(toAdd);
			resultsPanel.revalidate();
			resultsPanel.repaint();
		}
		else {
			resultsPanel.removeAll();
			
			resultsPanel.add(Box.createRigidArea(new Dimension(50,50)));
			resultsPanel.add(new JLabel("No events found!"));
		}
		resultsPanel.setBackground(Color.white);
		resultsPanel.revalidate();
		resultsPanel.repaint();
	}
	private void clearSearchBox(){
		searchBox.setText(SEARCHTEXT);
	}
	
	class ButtonActionListener implements ActionListener{
		
		public ButtonActionListener(){
		}
		public void actionPerformed(ActionEvent ae) {	
			CardLayout cl = (CardLayout)(cards.getLayout());
			if (ae.getSource()==backBut){
				System.out.println("Cancel Button Pressed");
				clearSearchBox(); // empty current contents
				cl.show(cards, "home");
			}
			else if (ae.getSource()==searchBut){
				//System.out.println("Search Not yet implemented");
				//System.out.println("Search term: "+searchBox.getText());
				search(searchBox.getText());
			}
		}

	}

}
